﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace NASA.BeAMartian.Controls
{
    [TemplatePart(Name = ClipRectangleTemplateName, Type = typeof(RectangleGeometry))]
    public class ClipContentControl : ContentControl
    {
        private const string ClipRectangleTemplateName = "ClipRectangleGeometry";
        private RectangleGeometry ClipRectangleTemplate = null;

        /// <summary> 
        /// Apply a template to the ClipContentControl.
        /// </summary>
        public override void OnApplyTemplate()
        {
            this.ClipRectangleTemplate = GetTemplateChild(ClipContentControl.ClipRectangleTemplateName) as RectangleGeometry;
            this.SizeChanged += new SizeChangedEventHandler(ClipContentControl_SizeChanged);
            base.OnApplyTemplate();
        }

        private void ClipContentControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (this.ClipRectangleTemplate != null)
            {
                this.ClipRectangleTemplate.Rect = new Rect(0, 0, this.ActualWidth, this.ActualHeight);
            }
        }
    }
}
